extends Area

export (String) var level
export (String) var spawn

signal on_level_change

func _ready():
	# Called when the node is added to the scene for the first time.
	# Initialization here
	
	pass


func _process(delta):
	for body in get_overlapping_bodies ( ):
		if body.name == "Player" :
			if Input.is_action_just_pressed("ui_accept") and Input.is_key_pressed(KEY_ENTER):
				loadLevel(body)
				
func loadLevel(body):
	if spawn != null:
		pass
		#print ("spawn found")
	if level != null:
		#print ("load map now!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
		#FIXME: put code in extra function used for saving data before exiting a map, 
		#maybe create signal that level changes soon and data has to be changed
		#when using a signal make sure that everything is saved before new level is loaded!
		emit_signal("on_level_change")
		
		var collectorNode = body.find_node("PlayerInventory", true, false)
		if collectorNode != null:
			#print ("collNode found in portal")
			get_node("/root/Save").save_node_data(collectorNode)
		get_node("/root/global").loadLevel("res://Scenes/World.tscn", level, spawn, true)
	
